home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1994 / MacHack 1994.toast / MacHack™ 1987-1994 / MacHack™ '92 / Hacks ’92 / Powerbook Compatibility INIT / Source / pwbk-cmp-init.p next >
Encoding:
Text File  |  1992-06-19  |  3.9 KB  |  186 lines  |  [TEXT/PJMM]

  1. program pwbk;
  2.  
  3.  
  4. { This is the source to Brian Gaeke's Powerbook Compatibility INIT. }
  5. { As you can see, it's not an INIT, but an appe.... but who cares?  }
  6. { This was finished DURING the hack contest. :)                     }
  7.  
  8. {$I-}
  9.  
  10.  
  11.     uses
  12.         Notification, Traps, Shutdown;
  13.     const
  14.         reserve_power = -16511;
  15.         very_little = -16512;
  16.         good_night = -16513;
  17.         battery_sicn = -16386;
  18.     var
  19.         starttime, nowtime, threshold: longint;
  20.         theID, iterations: Integer;
  21.         gNotify: NMRec;
  22.         gGoOn, notifySent: boolean;
  23.         event: eventrecord;
  24.         tempString: StringHandle;
  25.         theStr: Str255;
  26.         i: array[1..3] of str255;
  27.         gotevent: boolean;
  28.  
  29.     procedure NM_COMPLETION (theNMRec: NMRecPtr);
  30.         var
  31.             a5: longint;
  32.             err: integer;
  33.     begin
  34.         a5 := SetA5(theNMRec^.nmRefCon);
  35.         gGoOn := true;
  36.         err := NMRemove(theNMRec);
  37.         a5 := SetA5(a5);
  38.     end;
  39.  
  40.     procedure DoNotify (str_res_id: integer);
  41.         var
  42.             err: OsErr;
  43.     begin
  44.         gNotify.qtype := 8;
  45.         gNotify.nmmark := 0;
  46.         gNotify.nmsound := Handle(-1);
  47.         gNotify.nmresp := @NM_COMPLETION;
  48.         gNotify.nmicon := GetResource('SICN', battery_sicn);
  49.         gNotify.nmrefcon := SetCurrentA5;
  50.         gGoOn := false;
  51.         tempString := StringHandle(GetResource('STR ', str_res_id));
  52.         if tempString = nil then
  53.             begin
  54.                 SysBeep(5);
  55.                 ExitToShell;
  56.             end;
  57.         BlockMove(Ptr(tempString^), @theStr, Integer(tempString^^[0]) + 1);
  58.         ReleaseResource(Handle(tempString));
  59.         gNotify.nmstr := @theStr;
  60.         err := NMInstall(@gNotify);
  61.         if err <> noerr then
  62.             begin
  63.                 SysBeep(1);
  64.                 SysBeep(1);
  65.                 ExitToShell;
  66.             end;
  67.     end;
  68.  
  69.     function OnPortable: boolean;
  70.         var
  71.             err: OSErr;
  72.             result: longint;
  73. {Note: The following local routines are from the Compatibility Guidelines section}
  74. {of IM VI. They are gtt=GetTrapType, ntt=NumToolboxTraps, ok=TrapAvailable,}
  75. {and gestaltok=GestaltAvailable. I had to use $A1AD in gestaltok because the @&^$#(@#}
  76. {THINK Pascal interfaces don't have _Gestalt!? (or something....)}
  77.         function gtt (tr: integer): traptype;
  78.         begin
  79.             if BAND(tr, $800) > 0 then
  80.                 gtt := tooltrap
  81.             else
  82.                 gtt := ostrap;
  83.         end;
  84.         function ntt: integer;
  85.         begin
  86.             if NGetTrapAddress(_InitGraf, tooltrap) = NGetTrapAddress($AA6E, tooltrap) then
  87.                 ntt := $200
  88.             else
  89.                 ntt := $400;
  90.         end;
  91.         function ok (tr: integer): boolean;
  92.             var
  93.                 tt: traptype;
  94.         begin
  95.             tt := gtt(tr);
  96.             if tt = tooltrap then
  97.                 begin
  98.                     tr := BAND(tr, $07ff);
  99.                     if tr >= ntt then
  100.                         tr := _Unimplemented;
  101.                 end;
  102.             ok := ngettrapaddress(tr, tt) <> ngettrapaddress(_unimplemented, tooltrap);
  103.         end;
  104.         function gestaltok: boolean;
  105.         begin
  106.             gestaltok := ok($A1AD);
  107.         end;
  108.     begin
  109.         OnPortable := false;
  110.         if not gestaltok then
  111.             ExitToShell;
  112.         err := gestalt(gestaltPowerMgrAttr, result);
  113.         if err <> noErr then
  114.             ExitToShell;
  115.         if result > 0 then
  116.             onPortable := true;
  117.     end;
  118.  
  119.     function setNext (its: integer): longint;
  120.         var
  121.             t: str255;
  122.             tmp: longint;
  123.     begin
  124.         nowtime := tickcount;
  125.         case its of
  126.             1: 
  127.                 tmp := nowtime + 7200;
  128.             2: 
  129.                 tmp := nowtime + 3600;
  130.             3: 
  131.                 tmp := nowtime + 600;
  132.             4: 
  133.                 ShutDwnPower;
  134.         end;
  135.         setNext := tmp;
  136.     end;
  137.  
  138.     function setIDnext (it: integer): integer;
  139.     begin
  140.         case it of
  141.             1: 
  142.                 setIDnext := reserve_power;
  143.             2: 
  144.                 setIDnext := very_little;
  145.             3: 
  146.                 setIDnext := good_night;
  147.         end;
  148.     end;
  149.  
  150. begin
  151.     InitGraf(@ThePort);
  152.  
  153.     if OnPortable then
  154.         if not button then
  155.             ;
  156. {ExitToShell}
  157. {Originally, I had planned to run only on machines without a Power Mgr.}
  158. {I think we should just continue in the quest to confuse, and run anyway.}
  159.  
  160.     startTime := tickcount;
  161.     threshold := setNext(1);
  162.     theID := setIDnext(1);
  163.     iterations := 1;
  164.  
  165.     repeat
  166.         gotEvent := WaitNextEvent(0, event, 400, nil);
  167.  
  168.         nowtime := tickcount;
  169.         if nowtime >= threshold then
  170.             begin
  171.                 if not notifySent then
  172.                     begin
  173.                         doNotify(theID);
  174.                         notifySent := true;
  175.                     end;
  176.             end;
  177.         if gGoOn then
  178.             begin
  179.                 gGoOn := false;
  180.                 iterations := iterations + 1;
  181.                 threshold := setNext(iterations);
  182.                 theID := setIDnext(iterations);
  183.                 notifySent := false;
  184.             end;
  185.     until false;
  186. end.